<?php

namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;
use App\Models\ViewCount;
use App\Models\Order;
use Carbon\Carbon;

class Tickets extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('访问漏斗');
        $this->height(300);
        $this->chartHeight(200);
        $this->chartLabels('访客支付点击率');
        $this->dropdown([
            '1' => '今日',
            '-1' => '昨天',
            '7' => '最近 7 天',
            '10' => '最近 10 天',
            '30' => '最近 30 天',
            '100' => '最近 100 天',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $days = $request->get('option')??1;
        $date = Carbon::today();
        $y = false;
        if($days == -1){
            $y = true;
        }
        else if($days != 1 && $days != -1){
            $date = Carbon::today()->addDays(1-$days);
        }
        // 卡片底部
        $this->withFooter($date,$y);
        // 图表数据
        $this->withChart($date,$y);
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart($date,$y)
    {
        $rate = 0;
        if(!$y){
            $index = ViewCount::whereDate('created_at','>=',$date)->where('category',2)->pluck('openid');
            $index_uv = count(array_unique($index->toArray()));
            // $born_pay = ViewCount::whereDate('created_at','>=',$date)->where('category',4)->distinct()->select('openid')->count();
            $born_pay = Order::whereDate('created_at', '>=', $date)->pluck('openid');
            $born_pay = count(array_unique($born_pay->toArray()));
            if($index_uv!=0){
                $rate = bcmul(bcdiv($born_pay,$index_uv,4),100,2);
            }
        }
        else{
            $index = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->where('category',2)->pluck('openid');
            $index_uv = count(array_unique($index->toArray()));
            // $born_pay = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->where('category',4)->distinct()->select('openid')->count();
            $born_pay = Order::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->pluck('openid');
            $born_pay = count(array_unique($born_pay->toArray()));
            if($index_uv!=0){
                $rate = bcmul(bcdiv($born_pay,$index_uv,4),100,2);
            }
        }
        
        return $this->chart([
            'series' => [$rate],
        ]);
    }


    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($date,$y)
    {
        $index_pv = 0;
        $born_pv  = 0;
        $born_uv  = 0;
        $born_pay =0;
        $paid = 0;
        $pay_rate = 0;
        //不是昨天
        if(!$y){
            $index_pv = ViewCount::whereDate('created_at','>=',$date)->where('category',1)->count();
            $born = ViewCount::whereDate('created_at','>=',$date)->where('category',2)->pluck('openid');
            $born_pv  = count($born);
            $born_uv  = count(array_unique($born->toArray()));
            $born_pay = Order::whereDate('created_at','>=',$date)->count();
            $paid = Order::paid()->whereDate('paid_at', '>=', $date)->count();
        }
        else{
            $index_pv = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->where('category',1)->count();
            $born = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->where('category',2)->pluck('openid');
            $born_pv  = count($born);
            $born_uv  = count(array_unique($born->toArray()));
            $born_pay = Order::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->count();
            $paid = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-1))->whereDate('paid_at', '<', Carbon::today())->count();
        }
        if($born_pay!=0){
            $pay_rate = bcmul(bcdiv($paid,$born_pay,4),100,2);
        }
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>入口浏览量 pv</p>
        <span class="font-lg-1">{$index_pv}</span>
    </div>
    <div class="text-center">
        <p>落地浏览量 pv</p>
        <span class="font-lg-1">{$born_pv}</span>
    </div>
    <div class="text-center">
        <p>落地访客 uv</p>
        <span class="font-lg-1">{$born_uv}</span>
    </div>
    <div class="text-center">
        <p>支付点击量 pv</p>
        <span class="font-lg-1">{$born_pay}</span>
    </div>
    <div class="text-center">
        <p>支付成功(<span style='color:red;font-weight:bold;'>{$pay_rate}%</span>)</p>
        <span class="font-lg-1">{$paid}</span>
    </div>
</div>
HTML
        );
    }
}
